<?php

class m110918_032718_material_base extends CDbMigration {
	public function safeUp() {
		$this
			->createTable('erp_material_category',
				array(
					'id' => 'pk',
					'name' => 'string',
				));
		$this
			->createTable('erp_material',
				array(
					'id' => 'pk',
					'categoryId' => 'int',
					'name' => 'string',
				));
		$this->createIndex("material_material_category_id", 'erp_material', 'categoryId');
		$this
			->addForeignKey('fk_material_material_category', 'erp_material', 'categoryId',
				'erp_material_category', 'id', 'CASCADE', 'CASCADE');

		$this
			->createTable('erp_material_model',
				array(
					'id' => 'pk',
					'materialId' => 'int',
					'model' => 'string',
					'code' => 'string',
					'unit' => 'string',
					'referenceCode' => 'string',
					'note' => 'string',
				));
		$this->createIndex("material_model_material_id", 'erp_material_model', 'materialId');
		$this
			->addForeignKey('fk_material_model_material', 'erp_material_model', 'materialId',
				'erp_material', 'id', 'CASCADE', 'CASCADE');

		$this
			->createTable('erp_material_tag',
				array(
					'id' => 'pk',
					'name' => 'string',
				));
		$this
			->createTable('erp_material_tag_material',
				array(
					'id' => 'pk',
					'tagId' => 'int',
					'materialId' => 'int',
				));
		$this->createIndex("material_tag_tag_id", 'erp_material_tag_material', 'tagId');
		$this
			->addForeignKey('fk_material_tag_tag_id', 'erp_material_tag_material', 'tagId',
				'erp_material_tag', 'id', 'CASCADE', 'CASCADE');
		$this->createIndex("material_tag_material_id", 'erp_material_tag_material', 'materialId');
		$this
			->addForeignKey('fk_material_tag_material_id', 'erp_material_tag_material',
				'materialId', 'erp_material', 'id', 'CASCADE', 'CASCADE');
		$this
			->createTable('erp_material_alt_name',
				array(
					'id' => 'pk',
					'materialId' => 'int',
					'name' => 'string',
				));
		$this->createIndex("material_alt_name_material_id", 'erp_material_alt_name', 'materialId');
		$this
			->addForeignKey('fk_material_alt_name_material_id', 'erp_material_alt_name',
				'materialId', 'erp_material', 'id', 'CASCADE', 'CASCADE');
		$this->insert('erp_material_category', array(
				'name' => '钢材及金属制品',
			));
		$this->insert('erp_material_category', array(
				'name' => '木材、竹材及制品',
			));
		$this->insert('erp_material_category', array(
				'name' => '水泥及水泥制品',
			));
		$this->insert('erp_material_category', array(
				'name' => '砖、瓦及砌块',
			));
		$this->insert('erp_material_category', array(
				'name' => '砂（土、渣）石',
			));
		$this->insert('erp_material_category', array(
				'name' => '玻璃、沥青',
			));
		$this->insert('erp_material_category', array(
				'name' => '塑料（橡胶）制品',
			));
		$this->insert('erp_material_category', array(
				'name' => '油漆、化工',
			));
		$this->insert('erp_material_category', array(
				'name' => '其它土建工程材料',
			));

		$this->insert('erp_material_category', array(
				'name' => '给排水',
			));
		$this->insert('erp_material_category', array(
				'name' => '电气',
			));
		$this->insert('erp_material_category', array(
				'name' => '其它安装工程材料',
			));
		$this->insert('erp_material_category', array(
				'name' => '设备',
			));
		$this->insert('erp_material_category', array(
				'name' => '机械台班',
			));
		$this->insert('erp_material_category', array(
				'name' => '配合比',
			));
	}

	public function safeDown() {
		$this->dropTable('erp_material_alt_name');
		$this->dropTable('erp_material_tag_material');
		$this->dropTable('erp_material_tag');
		$this->dropTable('erp_material_model');
		$this->dropTable('erp_material');
		$this->dropTable('erp_material_category');
	}
}
